<template>
	<view class="status_wrap">
		<view class="status">{{mapIndentStatus[indent.status]}}</view>
		<view class="tips">
			{{mapIndentStateText[indent.status]}}
		</view>
		<view class="tips" style="color: #004EA2;" v-if="indent.status === -2">
			不通过原因: {{indent.no_pass_remark}}
		</view>
		<section style="display: flex;">
			<p class="one_more_order" v-if="[0,1,2].includes(indent.status)" @click="cancelIndent">取消订单</p>
			<template v-if="indent.status === 0">
				<p class="one_more_order" @click="continueOrder">继续点餐</p>
				<p class="one_more_order" @click="submitIndent">提交订单</p>
				<button class="one_more_order" open-type="share">分享</button>
			</template>
			<p class="one_more_order" v-else @click="oneMoreRrder">再来一单</p>
		</section>
	</view>
</template>

<script>
	const db = uniCloud.database()
	const indentDB = db.collection('dish-order-form')
	export default {
		name: 'indent-status-wrap',
		data() {
			return {
				mapIndentStateText: {
					0: '订单待提交',
					1: '订单正在审核中, 请稍后',
					2: '订单待制作, 请稍后',
					3: '订单已完成',
					'-1': '订单被取消',
					'-2': '订单审核不通过, 详情请联系客服',
				}
			}
		},
		props: ['indent'],
		computed: {
			mapIndentStatus() {
				return this.$store.state.order.mapIndentStatus
			}
		},
		methods: {
			async oneMoreRrder() {
				const flag = await this.$store.dispatch('veriftUserInfo')
				if (!flag) return
				await uni.switchTab({url: '/pages/order/order'})
				uni.$emit('oneMoreRrder', this.indent)
			},
			async submitIndent() {
				this.$store.dispatch('submitShareIndent', this.indent._id)
			},
			async continueOrder() {
				const flag = await this.$store.dispatch('veriftUserInfo')
				if (!flag) return
				uni.switchTab({
					url: '/pages/order/order'
				})
				uni.$emit('continueOrder', this.indent)
			},
			async cancelIndent() {
				console.log(this.indent._id);
				uni.showModal({
					title: '提示',
					content: '你确定取消订单吗',
					success: async () => {
						await indentDB.doc(this.indent._id).update({
							status: -1
						})
						uni.showToast({
							title: '取消成功',
							icon: 'success'
						})
						uni.$emit('refreshIndentList')
						this.$emit('reload')
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.status_wrap {
		min-height: 300rpx;
		background-color: #fff;
		border-radius: 12rpx;
		padding: 30rpx 40rpx;
		box-sizing: border-box;
		
		.status {
			height: 64rpx;
			font-size: 38rpx;
			font-weight: bold;
			line-height: 40rpx;
			border-bottom: 1px solid #f2f2f2;
		}
		.tips {
			font-size: 28rpx;
			color: #656565;
			margin: 30rpx 0;
		}
		.one_more_order {
			margin-right: 20rpx;
			width: 150rpx;
			height: 52rpx;
			border-radius: 10rpx;
			font-size: 24rpx;
			text-align: center;
			line-height: 52rpx;
			border: 1px solid $uni-theme-color;
			color: $uni-theme-color;
			background-color: white;
		}
		button[open-type="share"] {
			background: white;
			height: 56rpx;
			line-height: 56rpx;
			&::after {
				border: 0;
			}
		}
	}
</style>